/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/25 09:31
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {

    "use strict";

    var shopModule = angular.module('shop-module', ['ui.bootstrap']);
    shopModule.config(function ($httpProvider) {

        /**
         * 支持 form data 格式的数据 POST
         */
        $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

        /**
         * 重写angular的param方法，使angular使用jquery一样的数据序列化方式
         * The workhorse; converts an object to x-www-form-urlencoded serialization.
         *
         * @param {Object} obj
         * @return {String}
         */
        var param = function (obj) {
            var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

            for (name in obj) {
                value = obj[name];

                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                }
                else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                }
                else if (value !== undefined && value !== null)
                    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
            }

            return query.length ? query.substr(0, query.length - 1) : query;
        };

        // Override $http service's default transformRequest
        $httpProvider.defaults.transformRequest = [function (data) {
            return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
        }];

    });

    /**
     * 模态框
     */
    shopModule.factory('Modal', ['$uibModal', function ($uibModal) {

        var returnFunction = null;
        var Modal = function (options) {

            var _self = this, dialog = null;
            var icons = {
                alert: {
                    icon: 'fa-info-circle',
                    color: 'text-aqua'
                },
                success: {
                    icon: 'fa-check-circle',
                    color: 'text-green'
                },
                error: {
                    icon: 'fa-times-circle',
                    color: 'text-red'
                }
            };

            _self.open = function () {
                _self.dialog = $uibModal.open({
                    templateUrl: '/assets/library/bootstrap-ui/modal/template.html',
                    backdrop: 'static',
                    controller: function ($scope) {
                        options.buttons = options.buttons || [{name: "确定", cmd: "ok"}];
                        var type = options.type && icons[options.type] ? options.type : 'alert';

                        $scope.icons = icons[type];
                        $scope.type = type;
                        $scope.title = options.title || '';
                        $scope.content = options.content || '';

                        $scope.buttons = [];
                        angular.forEach((options.buttons), function (button) {
                            button.click = function () {
                                this.event = this.event || angular.noop;
                                if (this.event(self) !== false) {
                                    _self.dialog.close(this.cmd);
                                }
                            };
                            $scope.buttons.push(button);
                        });

                    }
                });
                return _self.dialog.result;
            };

        };

        var simpleModal = function (params, type) {
            var title, content, buttons, modal;
            if (typeof params === 'string') {
                title = params;
                content = '';
            } else {
                title = params.title;
                content = params.content;
                buttons = params.buttons
            }

            modal = new Modal({
                type: type,
                title: title,
                content: content,
                buttons: buttons
            });
            return modal.open();
        };

        returnFunction = function (data) {
            return new Modal(data);
        };

        returnFunction.success = function (params) {
            return simpleModal(params, 'success');
        };

        returnFunction.alert = function (params) {
            return simpleModal(params, 'alert');
        };

        returnFunction.error = function (params) {
            return simpleModal(params, 'error');
        };

        return returnFunction;

    }]);

    // 将其注册成 window 对象
    window.sm = shopModule;

}());

function GetQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

function WordCount(str) {
    return str ? Math.ceil(str.replace(/[\u1000-\uffff]{1}/g, 'aa').length / 2) : 0;
};